/**
 * APP端
 * 根据路径读取文件
 * @param {string} path - 文件路径
 */
export const useReadFileByPath = async (path: string) => {
	return new Promise((resolve) => {
		plus.io.resolveLocalFileSystemURL(
			path,
			(entry) => {
				entry.file(function (file) {
					resolve(file);
				});
			},
			(err) => {
				// err
				console.log(err);
			},
		);
	});
};

/**
 * APP端的图片和视频上传
 * @param cameraTitle - 相机权限提示标题
 * @param albumTitle - 相册权限提示标题
 * @param cameraTitleContent - 相机权限提示内容
 * @param albumContent - 相册权限提示内容
 * @param mode - 上传类型
 */
export const useUploadImageOrVideo = (
	mode: 'image' | 'video',
	uploadQty = 1,
	cameraTitle = '请允许我们使用相机权限',
	albumTitle = '请允许我们获取相册权限',
	cameraTitleContent = '允许“叮咚到家平台”在您使用该应用时访问您的相机吗？',
	albumContent = '允许“叮咚到家平台”在您使用该应用时访问您的相册吗？',
) => {
	return new Promise((resolve) => {
		const Manifest = plus.android.importClass('android.Manifest');
		const MainActivity = plus.android.runtimeMainActivity();

		if (MainActivity.checkSelfPermission(Manifest.permission.CAMERA) === -1) {
			uni.showModal({
				title: cameraTitle,
				content: cameraTitleContent,
				mask: true,
				icon: 'none',
				showCancel: true,
				cancelText: '我知道了',
				confirmText: '前往设置',
				success: (res) => {
					if (res.confirm) {
						uni.openAppAuthorizeSetting();
					}
				},
			});
			return;
		}

		if (
			MainActivity.checkSelfPermission(
				Manifest.permission.READ_EXTERNAL_STORAGE,
			) === -1
		) {
			uni.showModal({
				title: albumTitle,
				content: albumContent,
				mask: true,
				icon: 'none',
				showCancel: true,
				cancelText: '我知道了',
				confirmText: '前往设置',
				success: (res) => {
					if (res.confirm) {
						uni.openAppAuthorizeSetting();
					}
				},
			});
			return;
		}

		uni.showActionSheet({
			itemList: ['拍摄', '从相册选择'],
			success: (res: {tapIndex: number}) => {
				const uploadMode = res.tapIndex === 0 ? 'camera' : 'album';
				if (mode === 'image') {
					uni.chooseImage({
						count: uploadQty,
						sourceType: [uploadMode],
						success: async (res: {
							tempFiles: Array<File>;
							tempFilePaths: Array<string>;
						}) => {
							const result: Array<{
								file: any;
								path: string;
							}> = [];

							for (let i = 0; i < res.tempFiles.length; i++) {
								const tempFile = res.tempFiles[i];
								const readFileResult = await useReadFileByPath(tempFile.path);
								result.push({
									file: readFileResult,
									path: tempFile.path,
								});
							}

							resolve(result);
						},
					});
				}

				if (mode === 'video') {
					uni.chooseVideo({
						count: uploadQty,
						sourceType: [uploadMode],
						success: (res: {
							tempFilePath: string;
							tempFile: File;
							duration: number;
							size: number;
							height: number;
							width: number;
							name: string;
						}) => {
							useReadFileByPath(res.tempFilePath).then((readFileResult) => {
								resolve([
									{
										file: readFileResult,
										path: res.tempFilePath,
									},
								]);
							});
						},
					});
				}
			},
		});
	});
};
